
#import "BDProjectNavMapManager.h"
#import "BDMapView.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/UIView+React.h>

@interface BDProjectNavMapManager()<BMKMapViewDelegate,BMKLocationServiceDelegate>
@property (nonatomic, strong) BMKLocationService *localService;
@property (strong, nonatomic) BDMapView *mapView;
@end

@implementation BDProjectNavMapManager


-(BMKLocationService *)localService{
    if (!_localService) {
        _localService = [[BMKLocationService alloc] init];
        [_localService setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度
    }
    return _localService;
}

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE()
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)


-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return  self;
}

- (UIView *)view {
    
    _mapView = [BDMapView new];
    _mapView.delegate = self;
    // 设置地图类型
    [_mapView setMapType:BMKMapTypeStandard];
    // 普通定位模式
    _mapView.userTrackingMode = BMKUserTrackingModeHeading;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
    self.localService.delegate = self;
    [self.localService startUserLocationService];//用户开始定位
    
    
    UIImageView *locationImageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_location"]];
    
    locationImageView.frame = CGRectMake(kWidth(10), (kScreenHeight - (64) - (kHeight(38))- (kHeight(38))), kWidth(38), kHeight(38));
    locationImageView.backgroundColor = [UIColor clearColor];
    [_mapView addSubview:locationImageView];
    
    
    [_mapView setUserInteractionEnabled:YES];
    [locationImageView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLocationButtonTapped:)];
    [locationImageView addGestureRecognizer:tapRecg];
    return _mapView;
}


RCT_EXPORT_VIEW_PROPERTY(mapAnnotations, NSArray<BDMapAnnotation *>)
RCT_EXPORT_VIEW_PROPERTY(mapUpdateStatus, MapStatusInfo *)

//实现相关delegate

#pragma mark -- BMKLocationServiceDelegate
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    _mapView.showsUserLocation = YES;//显示定位图层
    //设置地图中心为用户经纬度
    [_mapView updateLocationData:userLocation];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BDMapAnnotation *anno = (BDMapAnnotation *)annotation;
        BMKAnnotationView *newAnnotationView;
        if(anno.projectCode != nil){
            
            
            newAnnotationView = [[BMKProjectNavAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"];
            ((BMKProjectNavAnnotationView *)newAnnotationView).delegate = self;
            ((BMKProjectNavAnnotationView *)newAnnotationView).bgIcon.image = [UIImage imageNamed:(anno.projectLevel != 1 ? @"icon_project_normal":@"icon_project_important")];
            [((BMKProjectNavAnnotationView *)newAnnotationView) displayProjectName:anno.projectName];
            
        }
        
        [mapView setCenterCoordinate:anno.coordinate];
        return newAnnotationView;
    }
    return nil;
}

-(void)BMKProjectNavAnnotationView:(BMKProjectNavAnnotationView *)sel clickViewWithData:(BDMapAnnotation *)data
{
    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=bd09ll",data.coordinate.latitude, data.coordinate.longitude,data.projectName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) {
        if(!success)
        {
            [self sendEvent:_mapView withTypeName:@"onStartBaiduMapFailed" params:@{}];
        }
    }];
}



-(void)onLocationButtonTapped:(UITapGestureRecognizer *)sender
{
    if(self.localService.userLocation.location != nil)
    {
        [_mapView setCenterCoordinate:self.localService.userLocation.location.coordinate animated:YES];
    }
}
-(void)sendEvent:(BDMapView *) mapView withTypeName:(NSString *)typeName params:(NSDictionary *) params {
    if (!mapView.onChange) {
        return;
    }
    mapView.onChange(@{
                       @"type": typeName,@"params":params
                       });
    
}

@end

